package com.superman.supermarket.client.composite.cart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.cart.sku.CartSkuModule;
import com.superman.supermarket.client.event.CartPageEvent;
import com.superman.supermarket.client.event.CartPageEvent.CartPageEventType;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.widget.button.CheckButton;

/**
 * 购物车店铺模块
 * 
 * @author superman
 * @version 2018年8月7日上午10:18:13
 */
public class CartStoreModule extends Composite implements IUIModule {

	interface CartStoreModuleUiBinder extends UiBinder<Widget, CartStoreModule> {
	}

	private static CartStoreModuleUiBinder uiBinder = GWT.create(CartStoreModuleUiBinder.class);

	private HandlerRegistration cartEventHR;
	@UiField
	CheckButton checkbtn;
	private List<Map<String, Object>> originSkuList;
	private List<Map<String, Object>> selectedTempSkuList = new ArrayList<Map<String, Object>>();
	@UiField
	FlowPanel skulistpanel, storepanel;
	private String storeId;

	@UiField
	Image storeimg;

	@UiField
	HTML storenamehtml, getcouponbtn;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月7日 上午10:18:15
	 */
	public CartStoreModule() {
		initWidget(uiBinder.createAndBindUi(this));

		storepanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				event.stopPropagation();
				ActivityJumpUrl.pushToStorePage(storeId);
			}
		}, ClickEvent.getType());
	}

	@UiHandler("checkbtn")
	void check(ClickEvent event) {
		// 店铺的点击事件,主要处理两个事情,一个遍历旗下sku更改选中状态,第二个触发选中店铺的事件用于activity层的监听处理
		checkChildren(checkbtn.isCheck());

		CartPageEvent cartPageEvent = new CartPageEvent(
				checkbtn.isCheck() ? CartPageEventType.CHECK_STORE : CartPageEventType.UNCHECK_STORE, this.storeId);
		XGinjector.INSTANCE.getEventbusController().fire(cartPageEvent);
	}

	private void checkChildren(boolean check) {
		selectedTempSkuList.clear();
		for (int i = 0; i < skulistpanel.getWidgetCount(); i++) {
			Widget widget = skulistpanel.getWidget(i);
			if (widget instanceof ICheckModule) {
				CartSkuModule cartSkuModule = (CartSkuModule) widget;
				if (check) {
					cartSkuModule.check();
					selectedTempSkuList.add(cartSkuModule.getData());
				} else {
					cartSkuModule.uncheck();
				}
			}
		}
	}

	private void compareSkuList() {
		boolean checkStatus = originSkuList.size() == selectedTempSkuList.size();

		checkbtn.setCheck(checkStatus);
		CartPageEvent cartPageEvent = new CartPageEvent(checkStatus ? CartPageEventType.CHECK_STORE : CartPageEventType.UNCHECK_STORE,
				this.storeId);
		XGinjector.INSTANCE.getEventbusController().fire(cartPageEvent);
	}

	@Override
	public Map<String, Object> getData() {
		Map<String, Object> skuStoreMap = new HashMap<String, Object>();
		skuStoreMap.put("STORE_ID", this.storeId);

		List<Map<String, Object>> skusList = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < skulistpanel.getWidgetCount(); i++) {
			Widget widget = skulistpanel.getWidget(i);
			if (widget instanceof CartSkuModule) {
				CartSkuModule cartSkuModule = (CartSkuModule) widget;
				if (cartSkuModule.isCheck()) {
					Map<String, Object> map = cartSkuModule.getData();
					map.remove("STORE_ID");
					skusList.add(map);
				}
			}
		}

		if (skusList.size() > 0) {
			skuStoreMap.put("SKU_LIST", skusList);
			return skuStoreMap;
		} else
			return null;

	}

	@UiHandler("getcouponbtn")
	void getStoreCoupon(ClickEvent event) {
		event.stopPropagation();
		ActivityJumpUrl.pushToStoreCouponListPage(storeId);
	}

	@Override
	protected void onAttach() {
		super.onAttach();
		// 监听商品选中和取消的事件.和当前店铺下的商品集合进行对比,如果全选中了,则改变店铺的选中状态
		cartEventHR = XGinjector.INSTANCE.getEventbusController().addHandler(CartPageEvent.TYPE, new CartPageEvent.CartPageEventHandler() {

			@Override
			public void checkAll(boolean check) {
				// 需要处理
				checkbtn.setCheck(check);
				checkChildren(check);

				CartPageEvent cartPageEvent = new CartPageEvent(
						checkbtn.isCheck() ? CartPageEventType.CHECK_STORE : CartPageEventType.UNCHECK_STORE, storeId);
				XGinjector.INSTANCE.getEventbusController().fire(cartPageEvent);
			}

			@Override
			public void checkSku(Map<String, Object> map) {
				// // 需要处理
				if (String.valueOf(map.get("STORE_ID")).equals(storeId)) {
					selectedTempSkuList.add(map);
					compareSkuList();
				}
			}

			@Override
			public void checkStore(String storeId) {
				// 不处理
			}

			@Override
			public void delete(String skuId) {
				// 不处理
			}

			@Override
			public void settlement() {
				// TODO Auto-generated method stub

			}

			@Override
			public void unCheckSku(Map<String, Object> map) {
				// 需要处理
				if (String.valueOf(map.get("STORE_ID")).equals(storeId)) {
					selectedTempSkuList.remove(map);
					compareSkuList();
				}
			}

			@Override
			public void uncheckStore(String storeId) {
				// 不处理
			}

			@Override
			public void updatePrice(Map<String, Object> map) {
				// 更新了数量,这里选中的数量也要更新,切记切记
				for (Map<String, Object> smap : selectedTempSkuList) {
					if (String.valueOf(smap.get("SKU_ID")).equals(String.valueOf(map.get("SKU_ID")))) {
						smap.put("SKU_NUM", map.get("SKU_NUM"));
						return;
					}
				}
			}

		});
	}

	@Override
	protected void onDetach() {
		super.onDetach();
		cartEventHR.removeHandler();
	}

	@Override
	public void setData(Map<String, Object> map) {
		boolean hasPlan = (int) Double.parseDouble(String.valueOf(map.get("HAS_PLAN"))) == 1;
		this.storeId = String.valueOf(map.get("STORE_ID"));
		storeimg.setUrl(String.valueOf(map.get("STORE_LOGO")));
		storenamehtml.setText(String.valueOf(map.get("STORE_NAME")));

		originSkuList = XJsonUtil.getList((JSONArray) map.get("SKU_LIST"));
		for (Map<String, Object> skuMap : originSkuList) {
			CartSkuModule cartSkuModule = new CartSkuModule();
			cartSkuModule.setData(skuMap);
			cartSkuModule.setStoreId(storeId);
			skulistpanel.add(cartSkuModule);
		}

		getcouponbtn.setVisible(hasPlan);
	}

}
